package contacts530.application;


import java.awt.BorderLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import contacts530.po.Room;

import java.sql.*;
import javax.swing.JOptionPane;

public class StudentView extends JFrame implements ActionListener {

	private JPanel Panel, Panel2;
	private JButton Add, Delete, Update, select, show;
	private JComboBox sort, way;
	private  JTextField message;
	private  JScrollPane gdt;
	private JLabel tip;
	private JTable bg1;
	private Select sel;
	private  Vector title, massage;
    PreparedStatement ps = null; 
    Connection ct = null;
    ResultSet rs = null;
//	private JComboBox<String> roomNocomBox;
    public StudentView() {
        Panel2 = new JPanel(); 
//        roomNocomBox=new JComboBox<String>();
//        List rooms =((Object) ContactSevice()).findAllRoom();
//        System.out.println(rooms.size());
//        for(Room room ：rooms) {
//        	roomNocomBox.addItem(String.valueOf(room.getRoom_no()));
//        	roomNocomBox.getSelectedItem().toString();
//        	
//        }
        //     tip = new JLabel("please input choice");
        String[] choice1 = {"查询方式", "姓名", "宿舍名", "年龄","生日", "手机", "email","qq"};
        way = new JComboBox(choice1); 
        way.addActionListener(this);
        message = new JTextField(10);
        select = new JButton("查询");
        show = new JButton("显示全部信息");
        show.addActionListener(this);
        select.addActionListener(this);
        Panel2.add(way);
        Panel2.add(message);
        Panel2.add(select);
        Panel2.add(show);
        sel = new Select();
        bg1 = new JTable(sel);
        gdt = new JScrollPane(bg1);
        Panel = new JPanel();
        Add = new JButton("添加");
        Add.addActionListener(this);
        Delete = new JButton("删除");
        Delete.addActionListener(this);
        Update = new JButton("更新");
        Update.addActionListener(this);
        Panel.add(Add);
        Panel.add(Delete);
        Panel.add(Update);
        this.add(gdt);
        this.add(Panel, BorderLayout.SOUTH);
        this.add(Panel2, BorderLayout.NORTH);
        
        this.setTitle("班级通讯录管理");
        this.setSize(700, 720);
        this.setLocationRelativeTo(null);

        this.setDefaultCloseOperation(3);
        this.setResizable(false);
        this.setVisible(true);

    }

    /**
	 * @return
	 */
	private Object ContactSevice() {
		// TODO Auto-generated method stub
		return null;
	}

	public static void main(String[] args) {
        new StudentView();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        String selway = (String) way.getSelectedItem();
        if (s.equals("查询")) {
            String choice = message.getText().trim();
            if (selway.equals("姓名")) {
                String sql = "select * from student where name='" + choice + "'";
                sel = new Select(sql);
                bg1.setModel(sel);
            } else if (selway.equals("宿舍名")) {
                String sql = "select * from student where gender ='" + choice + "'";
                Select sq = new Select(sql);
                bg1.setModel(sq);
            } else if (selway.equals("年龄")) {
                String sql = "select * from student where age='" + choice + "'";
                Select sq = new Select(sql);
                bg1.setModel(sq);
            } else if (selway.equals("生日")) {
                String sql = "select * from student where dbo='" + choice + "'";
                Select sq = new Select(sql);
                bg1.setModel(sq);
            } else if (selway.equals("手机")) {
                String sql = "select * from student where phone='" + choice + "'";
                Select sq = new Select(sql);
                bg1.setModel(sq);
            } else if (selway.equals("Email")) {
                String sql = "select * from student where email='" + choice + "'";
                Select sq = new Select(sql);
                bg1.setModel(sq);
            } else if (selway.equals("QQ")) {
                String sql = "select * from student where qq='" + choice + "'";
                Select sq = new Select(sql);
                bg1.setModel(sq);
            } else {
                JOptionPane.showMessageDialog(this, "请选择查找方式");
                return;
            }
        } else if (s.equals("添加")) {
            addStudent ad = new addStudent(this, "添加人员信息",true);
            sel = new Select();
            bg1.setModel(sel);
        } else if (s.equals("显示全部信息")) {
            String sql = "select * from student";
            sel = new Select(sql);
            bg1.setModel(sel);
        } else if (s.equals("更新")) {
            int iii = this.bg1.getSelectedRow();
            System.out.println(iii);
            if (iii == -1) {
                JOptionPane.showMessageDialog(this, "请选中要更新的行数");
                return;
            }
            Update u = new Update(this, "修改人员信息", true, sel, iii);
            sel = new Select();
            bg1.setModel(sel);
        } else if (s.equals("删除")) {
            int ii = this.bg1.getSelectedRow();
            System.out.println(ii);
            if (ii == -1) {
                JOptionPane.showMessageDialog(this, "请选择要删除的行数");
                return;
            }
            String st = (String) sel.getValueAt(ii, 0);
            System.out.println(st);
            Delete delete = new Delete(st); 
            sel = new Select();
            bg1.setModel(sel);
        } 
    }
}
